//
//  LXEmoticonToolbar.swift
//  weibo
//
//  Created by lixin on 16/4/25.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

let tagNumber = 100

enum LXEmoticonToolbarType: Int {
    
    //默认表情
    case Default = 0
    
    //emoji
    case Emoji = 1
    
    //浪小花
    case Lxh = 2
}

class LXEmoticonToolbar: UIView {
    
    var LXEmoticonToolbarClosure: ((type: LXEmoticonToolbarType) -> ())?
    
    //记录上次点击的按钮
    var currentBtn: UIButton?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setEmoticonCurrentButton(section: Int){
    
        let button = viewWithTag(section + tagNumber) as! UIButton
        
        if button == currentBtn {
            
            return
        }
        
        button.selected = true
        
        currentBtn?.selected = false
        
        currentBtn = button
    }
    
    private func setUI(){
        
        let defaultBtn = addChildButton("compose_emotion_table_left", title: "默认", type: .Default)
        
        let emojiBtn = addChildButton("compose_emotion_table_mid", title: "Emoji", type: .Emoji)
        
        let lxhBtn = addChildButton("compose_emotion_table_right", title: "浪小花", type: .Lxh)
        
        defaultBtn.snp_makeConstraints { (make) in
            
            make.leading.top.bottom.equalTo(self)
            
            make.width.equalTo(emojiBtn)
        }
        
        emojiBtn.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            
            make.leading.equalTo(defaultBtn.snp_trailing)
            
            make.width.equalTo(lxhBtn)
        }
        
        lxhBtn.snp_makeConstraints { (make) in
            
            make.top.bottom.trailing.equalTo(self)
            
            make.leading.equalTo(emojiBtn.snp_trailing)
        }
        
        defaultBtn.selected = true
        
        currentBtn = defaultBtn
        
    }
    
    //MARK: - 添加按钮的公共方法
    func addChildButton(imgName: String, title: String, type: LXEmoticonToolbarType) -> UIButton{
    
        let button = UIButton()
        
        button.tag = type.rawValue + tagNumber
        
        button.addTarget(self, action: #selector(LXEmoticonToolbar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "\(imgName)_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "\(imgName)_selected"), forState: UIControlState.Selected)
        
        button.setTitle(title, forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        addSubview(button)
        
        return button
    }
    
    //MARK: - 点击事件
    func buttonClick(button: UIButton){
    
        if button == currentBtn {
            
            return
        }
        
        button.selected = true
        
        currentBtn?.selected = false
        
        currentBtn = button
        
        LXEmoticonToolbarClosure?(type: LXEmoticonToolbarType(rawValue: button.tag - tagNumber)!)
    }
}
